//
//  UTSimulateRollViewController.swift
//  UniversalTool
//
//  Created by LGB on 2025/3/10.
//

import UIKit

class UTSimulateRollViewController: UTBaseViewController {
    
    var handler:((UTSimulateModel) -> Void)?
    var model: UTSimulateModel?
    @IBOutlet weak var durationSlider: UISlider!
    @IBOutlet weak var durationLabel: UILabel!
    @IBOutlet weak var durationCenterX: NSLayoutConstraint!
    @IBOutlet weak var tenPercentButton: UIButton!
    @IBOutlet weak var twentyPercentButton: UIButton!
    @IBOutlet weak var fiftyPercentButton: UIButton!
    @IBOutlet weak var eightyPercentButton: UIButton!
    @IBOutlet weak var hundredPercentButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(white: 0, alpha: 0.3)
        durationSlider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
        if let model = model {
            durationSlider.value = Float(model.rollInterval)
            durationLabel.text = "\(model.rollInterval)秒"
            durationCenterX.constant = durationSlider.frame.origin.x + CGFloat(durationSlider.value / durationSlider.maximumValue) * durationSlider.frame.width - durationSlider.frame.origin.x - durationSlider.frame.width / 2 + 5
            switch model.rollArea {
            case 0.1:
                tenPercentButton.isSelected = true
                twentyPercentButton.isSelected = false
                fiftyPercentButton.isSelected = false
                eightyPercentButton.isSelected = false
                hundredPercentButton.isSelected = false
            case 0.2:
                tenPercentButton.isSelected = false
                twentyPercentButton.isSelected = true
                fiftyPercentButton.isSelected = false
                eightyPercentButton.isSelected = false
                hundredPercentButton.isSelected = false
            case 0.5:
                tenPercentButton.isSelected = false
                twentyPercentButton.isSelected = false
                fiftyPercentButton.isSelected = true
                eightyPercentButton.isSelected = false
                hundredPercentButton.isSelected = false
            case 0.8:
                tenPercentButton.isSelected = false
                twentyPercentButton.isSelected = false
                fiftyPercentButton.isSelected = false
                eightyPercentButton.isSelected = true
                hundredPercentButton.isSelected = false
            case 1.0:
                tenPercentButton.isSelected = false
                twentyPercentButton.isSelected = false
                fiftyPercentButton.isSelected = false
                eightyPercentButton.isSelected = false
                hundredPercentButton.isSelected = true
            default:
                break
            }
        }
    }
    
    
    @IBAction func didClose(_ sender: Any) {
        dismiss(animated: false)
    }
    
    @objc func sliderValueChanged(_ sender: UISlider) {
        updateValueLabelPosition(sender)
    }
    
    func updateValueLabelPosition(_ sender: UISlider) {
        let value = Int(sender.value)
        if sender == durationSlider {
            guard let model = model else { return }
            model.rollInterval = value
            let centerX = durationSlider.frame.origin.x + CGFloat(durationSlider.value / durationSlider.maximumValue) * durationSlider.frame.width
            durationLabel.text = "\(value)秒"
            durationCenterX.constant = centerX - durationSlider.frame.origin.x - durationSlider.frame.width / 2 + 5
        }
    }
    
    @IBAction func didSelectRollingArea(_ sender: UIButton) {
        guard let model = model else { return }
        if sender.isSelected { return }
        sender.isSelected = !sender.isSelected
        if sender == tenPercentButton{
            model.rollArea = 0.1
            twentyPercentButton.isSelected = false
            fiftyPercentButton.isSelected = false
            eightyPercentButton.isSelected = false
            hundredPercentButton.isSelected = false
        }else if sender == twentyPercentButton{
            model.rollArea = 0.2
            tenPercentButton.isSelected = false
            fiftyPercentButton.isSelected = false
            eightyPercentButton.isSelected = false
            hundredPercentButton.isSelected = false
        }else if sender == fiftyPercentButton{
            model.rollArea = 0.5
            tenPercentButton.isSelected = false
            twentyPercentButton.isSelected = false
            eightyPercentButton.isSelected = false
            hundredPercentButton.isSelected = false
        }else if sender == eightyPercentButton{
            model.rollArea = 0.8
            tenPercentButton.isSelected = false
            twentyPercentButton.isSelected = false
            fiftyPercentButton.isSelected = false
            hundredPercentButton.isSelected = false
        }else if sender == hundredPercentButton{
            model.rollArea = 1.0
            tenPercentButton.isSelected = false
            twentyPercentButton.isSelected = false
            fiftyPercentButton.isSelected = false
            eightyPercentButton.isSelected = false
        }
    }
    
    @IBAction func save(_ sender: UIButton) {
        if let handler = handler {
            guard let model = model else { return }
            handler(model)
        }
        dismiss(animated: false)
    }
    
}
